package com.cstor.liang.service;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.cstor.liang.mapper.UserMessageMapper;
import com.cstor.liang.pojo.entity.UserMessage;
import com.cstor.liang.pojo.other.UserMessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/6.
 */
@Service
@Transactional
public class UserMessageService extends BaseService<UserMessage>{

    @Autowired
    private UserMessageMapper userMessageMapper;

    /**
     * 获取未读消息数目
     * @param userId 用户id
     * @return
     */
    @Transactional(readOnly = true)
    public int getUnreadCount(Long userId){
        Example example = new Example(UserMessage.class);
        example.createCriteria()
                .andEqualTo("userId", userId)
                .andEqualTo("haveRead", 0);
        return userMessageMapper.selectCountByExample(example);
    }

    /**
     * 获取用户消息
     * @param userId 用户id
     * @param pageIndex 页码
     * @param pageSize 数量
     * @return
     */
    @Transactional(readOnly = true)
    public List<UserMessage> listByUserId(Long userId, Integer pageIndex, Integer pageSize){
        PageHelper.startPage(pageIndex, pageSize);
        Example example = new Example(UserMessage.class);
        example.setOrderByClause(" create_time DESC");
        example.createCriteria()
                .andEqualTo("userId", userId);
        return userMessageMapper.selectByExample(example);
    }

    /**
     * 设置用户消息已读
     * @param userId 被设置的用户id
     * @param userMessageId 被设置的消息id
     */
    public void setToReaded(Long userId, Long userMessageId){
        userMessageMapper.setToReaded(userMessageId, userId);
    }

    /**
     * 设置全部消息已读
     * @param userId 被设置的用户id
     */
    public void setAllToReaded(Long userId){
        userMessageMapper.setAllToReaded(userId);
    }

    /**
     * 新增用户消息（批量插入）
     * @param userMessageVo 消息内容
     * @param userIds 多个用户id，以 ',' 分隔
     */
    public int add(UserMessageVo userMessageVo, String userIds){
        //切割字符串
        String[] ids = userIds.split(",");
        //构造用户消息列表
        List<UserMessage> list = Lists.newArrayListWithCapacity(ids.length);
        Date now = new Date();
        for (String id : ids){
            UserMessage um = new UserMessage(userMessageVo);
            um.setCreateTime(now);
            um.setHaveRead(0);
            um.setUserId(Integer.parseInt(id));
            list.add(um);
        }

        //批量插入
        return userMessageMapper.insertList(list);
    }

    /**
     * 删除用户消息（批量删除），非指定用户的消息删除无效
     * @param userMessageIds 被删除的消息id
     * @param userId 被删除消息的用户id
     * @return
     */
    public int delete(String userMessageIds, Long userId){
        //切割字符串
        String[] ids = userMessageIds.split(",");

        //批量删除
        Example example = new Example(UserMessage.class);
        example.createCriteria()
                .andIn("id", Lists.newArrayList(ids))
                .andEqualTo("userId", userId);
        return userMessageMapper.deleteByExample(example);
    }
}
